package com.nuoniu.sibanyun.service.crm.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.SysCompany;
import com.nuoniu.sibanyun.entity.SysUser;
import com.nuoniu.sibanyun.entity.crm.CrmGoods;
import com.nuoniu.sibanyun.entity.crm.CrmSecretKey;
import com.nuoniu.sibanyun.entity.crm.CrmWebConfig;
import com.nuoniu.sibanyun.entity.msg.SysMsg;
import com.nuoniu.sibanyun.mapper.SysCompanyMapper;
import com.nuoniu.sibanyun.mapper.SysUserMapper;
import com.nuoniu.sibanyun.mapper.crm.CrmGoodsMapper;
import com.nuoniu.sibanyun.mapper.crm.CrmSecretKeyMapper;
import com.nuoniu.sibanyun.mapper.crm.CrmWebConfigMapper;
import com.nuoniu.sibanyun.service.crm.ICrmSecretKeyService;
import com.nuoniu.sibanyun.service.msg.ISysMsgService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 秘钥管理 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-07-17
 */
@Service
public class CrmSecretKeyServiceImpl extends ServiceImpl<CrmSecretKeyMapper, CrmSecretKey> implements ICrmSecretKeyService {

    @Resource
    private CrmSecretKeyMapper secretKeyMapper;
    @Resource
    private SysCompanyMapper companyMapper;
     @Resource
    private SysUserMapper userMapper;
     @Resource
    private CrmWebConfigMapper webConfigMapper;
     @Resource
    private CrmGoodsMapper goodsMapper;
     @Resource
    private ISysMsgService msgService;

    @Override
    public Page<CrmSecretKey> pageGoods(Page pages, CrmSecretKey secretKey) {
        CrmSecretKey crmGoods = new CrmSecretKey();
        QueryWrapper<CrmSecretKey> queryWrapper = new QueryWrapper<>(crmGoods);
        if(secretKey.getStatus() != null) {
            queryWrapper.eq("status", secretKey.getStatus());
        }
        if(secretKey.getSecretKey() != null) {
            queryWrapper.like("secret_key", secretKey.getSecretKey());
        }
        queryWrapper.orderByDesc("create_date");
        return page(pages, queryWrapper);
    }

    @Override
    public String getBySecretKey() {
        String key = StringUtils.getUUID(4,5);
        CrmSecretKey secretKey = secretKeyMapper.getBySecretKey(key);
        if(secretKey == null) {
            return key;
        }else {
            getBySecretKey();
            return null;
        }
    }

    @Override
    public CrmSecretKey getSecretKey(String secretKey) {
        return secretKeyMapper.getBySecretKey(secretKey);
    }

    @Override
    public void checkSecretKey() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        List<SysMsg> msgList = new ArrayList<>();
        CrmWebConfig config = webConfigMapper.selectById("1");
        List<CrmSecretKey> secretKeyList = secretKeyMapper.getSecretKeyEnd();
        for(CrmSecretKey secretKey : secretKeyList) {
            List<SysCompany> companyList = companyMapper.getBySecretKye(secretKey);
            for(SysCompany company : companyList) {
                Integer roleId = 9;
                if(company.getGoodsId() != null) {
                    CrmGoods goods = goodsMapper.selectById(company.getGoodsId());
                    roleId = goods.getRoleId();
                }

                //处理该公司正在使用的用户
                SysUser user = userMapper.getSysManager(company.getId().intValue(), roleId);
                List<UserInfo> userList = userMapper.getUserList(company.getId().intValue());
                if(userList.size() > config.getFreeUsers() + company.getCompanyUser()) {
                    //停用用户31
                    userMapper.updateStatus(company.getId(), userList.size(), config.getFreeUsers() + company.getCompanyUser() - 1, user.getId());
                }

                company.setCompanyUser(0);
                companyMapper.updateById(company);

                SysMsg msg = new SysMsg();
                msg.setType(0);
                msg.setCompanyId(company.getId().intValue());
                msg.setMenuHref("/views/system/info/companyInfo");
                msg.setUserId(user.getId().intValue());
                msg.setUserName(user.getRealName());
                msg.setTitle("授权密钥过期提醒");
                msg.setContent("你使用的秘钥已于" + df.format(secretKey.getEndDate()) + "过期，请前往查看！");
                msgList.add(msg);
            }

            //停用密钥
            secretKey.setStatus(2);
            secretKeyMapper.updateById(secretKey);

            //发送通知
            msgService.saveBatch(msgList);
        }
    }
}
